﻿using System;
using System.Data;
using System.Data.SQLite;
using System.Text;
using EIM_Accounting.Common.DBUtility;//Please add references

namespace Accounting.DAL
{
    /// <summary>
    /// 数据访问类:Contract
    /// </summary>
    public partial class Contract
    {
        public Contract()
        { }

        #region Method

        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId()
        {
            return DbHelperSQLite.GetMaxID("ID", "Contract");
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int ID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from Contract");
            strSql.Append(" where ID=@ID");
            SQLiteParameter[] parameters = {
                    new SQLiteParameter("@ID", DbType.Int32,4)
};
            parameters[0].Value = ID;

            return DbHelperSQLite.Exists(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(Accounting.Model.Contract model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into Contract(");
            strSql.Append("SignComp,PlanStartDate,PlanEndDate,SingleMoney,TotalMoney,FinishFlag,RealStartDate,RealEndDate,Remark,NormalAddFee,WeekEndAddFee,HolidayAddFee)");
            strSql.Append(" values (");
            strSql.Append("@SignComp,@PlanStartDate,@PlanEndDate,@SingleMoney,@TotalMoney,@FinishFlag,@RealStartDate,@RealEndDate,@Remark,@NormalAddFee,@WeekEndAddFee,@HolidayAddFee)");
            strSql.Append(";select LAST_INSERT_ROWID()");
            SQLiteParameter[] parameters = {
                    new SQLiteParameter("@SignComp", DbType.String),
                    new SQLiteParameter("@PlanStartDate", DbType.String),
                    new SQLiteParameter("@PlanEndDate", DbType.String),
                    new SQLiteParameter("@SingleMoney", DbType.Decimal,4),
                    new SQLiteParameter("@TotalMoney", DbType.Decimal,4),
                    new SQLiteParameter("@FinishFlag", DbType.String),
                    new SQLiteParameter("@RealStartDate", DbType.String),
                    new SQLiteParameter("@RealEndDate", DbType.String),
                    new SQLiteParameter("@Remark", DbType.String),
                    new SQLiteParameter("@NormalAddFee", DbType.Decimal,4),
                    new SQLiteParameter("@WeekEndAddFee", DbType.Decimal,4),
                    new SQLiteParameter("@HolidayAddFee", DbType.Decimal,4)};
            parameters[0].Value = model.SignComp;
            parameters[1].Value = model.PlanStartDate;
            parameters[2].Value = model.PlanEndDate;
            parameters[3].Value = model.SingleMoney;
            parameters[4].Value = model.TotalMoney;
            parameters[5].Value = model.FinishFlag;
            parameters[6].Value = model.RealStartDate;
            parameters[7].Value = model.RealEndDate;
            parameters[8].Value = model.Remark;
            parameters[9].Value = model.NormalAddFee;
            parameters[10].Value = model.WeekEndAddFee;
            parameters[11].Value = model.HolidayAddFee;

            object obj = DbHelperSQLite.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Accounting.Model.Contract model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update Contract set ");
            strSql.Append("SignComp=@SignComp,");
            strSql.Append("PlanStartDate=@PlanStartDate,");
            strSql.Append("PlanEndDate=@PlanEndDate,");
            strSql.Append("SingleMoney=@SingleMoney,");
            strSql.Append("TotalMoney=@TotalMoney,");
            strSql.Append("FinishFlag=@FinishFlag,");
            strSql.Append("RealStartDate=@RealStartDate,");
            strSql.Append("RealEndDate=@RealEndDate,");
            strSql.Append("Remark=@Remark,");
            strSql.Append("NormalAddFee=@NormalAddFee,");
            strSql.Append("WeekEndAddFee=@WeekEndAddFee,");
            strSql.Append("HolidayAddFee=@HolidayAddFee");
            strSql.Append(" where ID=@ID");
            SQLiteParameter[] parameters = {
                    new SQLiteParameter("@SignComp", DbType.String),
                    new SQLiteParameter("@PlanStartDate", DbType.String),
                    new SQLiteParameter("@PlanEndDate", DbType.String),
                    new SQLiteParameter("@SingleMoney", DbType.Decimal,4),
                    new SQLiteParameter("@TotalMoney", DbType.Decimal,4),
                    new SQLiteParameter("@FinishFlag", DbType.String),
                    new SQLiteParameter("@RealStartDate", DbType.String),
                    new SQLiteParameter("@RealEndDate", DbType.String),
                    new SQLiteParameter("@Remark", DbType.String),
                    new SQLiteParameter("@NormalAddFee", DbType.Decimal,4),
                    new SQLiteParameter("@WeekEndAddFee", DbType.Decimal,4),
                    new SQLiteParameter("@HolidayAddFee", DbType.Decimal,4),
                    new SQLiteParameter("@ID", DbType.Int32,8)};
            parameters[0].Value = model.SignComp;
            parameters[1].Value = model.PlanStartDate;
            parameters[2].Value = model.PlanEndDate;
            parameters[3].Value = model.SingleMoney;
            parameters[4].Value = model.TotalMoney;
            parameters[5].Value = model.FinishFlag;
            parameters[6].Value = model.RealStartDate;
            parameters[7].Value = model.RealEndDate;
            parameters[8].Value = model.Remark;
            parameters[9].Value = model.NormalAddFee;
            parameters[10].Value = model.WeekEndAddFee;
            parameters[11].Value = model.HolidayAddFee;
            parameters[12].Value = model.ID;

            int rows = DbHelperSQLite.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int ID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from Contract ");
            strSql.Append(" where ID=@ID");
            SQLiteParameter[] parameters = {
                    new SQLiteParameter("@ID", DbType.Int32,4)
};
            parameters[0].Value = ID;

            int rows = DbHelperSQLite.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string IDlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from Contract ");
            strSql.Append(" where ID in (" + IDlist + ")  ");
            int rows = DbHelperSQLite.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Accounting.Model.Contract GetModel(int ID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ID,SignComp,PlanStartDate,PlanEndDate,SingleMoney,TotalMoney,FinishFlag,RealStartDate,RealEndDate,Remark,NormalAddFee,WeekEndAddFee,HolidayAddFee from Contract ");
            strSql.Append(" where ID=@ID");
            SQLiteParameter[] parameters = {
                    new SQLiteParameter("@ID", DbType.Int32,4)
};
            parameters[0].Value = ID;

            Accounting.Model.Contract model = new Accounting.Model.Contract();
            DataSet ds = DbHelperSQLite.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["ID"] != null && ds.Tables[0].Rows[0]["ID"].ToString() != "")
                {
                    model.ID = int.Parse(ds.Tables[0].Rows[0]["ID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["SignComp"] != null && ds.Tables[0].Rows[0]["SignComp"].ToString() != "")
                {
                    model.SignComp = ds.Tables[0].Rows[0]["SignComp"].ToString();
                }
                if (ds.Tables[0].Rows[0]["PlanStartDate"] != null && ds.Tables[0].Rows[0]["PlanStartDate"].ToString() != "")
                {
                    model.PlanStartDate = ds.Tables[0].Rows[0]["PlanStartDate"].ToString();
                }
                if (ds.Tables[0].Rows[0]["PlanEndDate"] != null && ds.Tables[0].Rows[0]["PlanEndDate"].ToString() != "")
                {
                    model.PlanEndDate = ds.Tables[0].Rows[0]["PlanEndDate"].ToString();
                }
                if (ds.Tables[0].Rows[0]["SingleMoney"] != null && ds.Tables[0].Rows[0]["SingleMoney"].ToString() != "")
                {
                    model.SingleMoney = decimal.Parse(ds.Tables[0].Rows[0]["SingleMoney"].ToString());
                }
                if (ds.Tables[0].Rows[0]["TotalMoney"] != null && ds.Tables[0].Rows[0]["TotalMoney"].ToString() != "")
                {
                    model.TotalMoney = decimal.Parse(ds.Tables[0].Rows[0]["TotalMoney"].ToString());
                }
                if (ds.Tables[0].Rows[0]["FinishFlag"] != null && ds.Tables[0].Rows[0]["FinishFlag"].ToString() != "")
                {
                    model.FinishFlag = ds.Tables[0].Rows[0]["FinishFlag"].ToString();
                }
                if (ds.Tables[0].Rows[0]["RealStartDate"] != null && ds.Tables[0].Rows[0]["RealStartDate"].ToString() != "")
                {
                    model.RealStartDate = ds.Tables[0].Rows[0]["RealStartDate"].ToString();
                }
                if (ds.Tables[0].Rows[0]["RealEndDate"] != null && ds.Tables[0].Rows[0]["RealEndDate"].ToString() != "")
                {
                    model.RealEndDate = ds.Tables[0].Rows[0]["RealEndDate"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Remark"] != null && ds.Tables[0].Rows[0]["Remark"].ToString() != "")
                {
                    model.Remark = ds.Tables[0].Rows[0]["Remark"].ToString();
                }
                if (ds.Tables[0].Rows[0]["NormalAddFee"] != null && ds.Tables[0].Rows[0]["NormalAddFee"].ToString() != "")
                {
                    model.NormalAddFee = decimal.Parse(ds.Tables[0].Rows[0]["NormalAddFee"].ToString());
                }
                if (ds.Tables[0].Rows[0]["WeekEndAddFee"] != null && ds.Tables[0].Rows[0]["WeekEndAddFee"].ToString() != "")
                {
                    model.WeekEndAddFee = decimal.Parse(ds.Tables[0].Rows[0]["WeekEndAddFee"].ToString());
                }
                if (ds.Tables[0].Rows[0]["HolidayAddFee"] != null && ds.Tables[0].Rows[0]["HolidayAddFee"].ToString() != "")
                {
                    model.HolidayAddFee = decimal.Parse(ds.Tables[0].Rows[0]["HolidayAddFee"].ToString());
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  ID,SignComp,PlanStartDate,PlanEndDate,SingleMoney,TotalMoney,FinishFlag,RealStartDate,RealEndDate,Remark,NormalAddFee,WeekEndAddFee,HolidayAddFee ");
            strSql.Append(" FROM Contract ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQLite.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SQLiteParameter[] parameters = {
                    new SQLiteParameter("@tblName", DbType.VarChar, 255),
                    new SQLiteParameter("@fldName", DbType.VarChar, 255),
                    new SQLiteParameter("@PageSize", DbType.Int32),
                    new SQLiteParameter("@PageIndex", DbType.Int32),
                    new SQLiteParameter("@IsReCount", DbType.bit),
                    new SQLiteParameter("@OrderType", DbType.bit),
                    new SQLiteParameter("@strWhere", DbType.VarChar,1000),
                    };
            parameters[0].Value = "Contract";
            parameters[1].Value = "ID";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;
            return DbHelperSQLite.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion Method

        internal void DiscardModel(Model.Contract contractModel)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update Contract set ");

            strSql.Append("FinishFlag=@FinishFlag ");

            strSql.Append(" where ID=@ID");
            SQLiteParameter[] parameters = {
                    new SQLiteParameter("@FinishFlag", DbType.String),

                    new SQLiteParameter("@ID", DbType.Int32,8)};

            parameters[0].Value = contractModel.FinishFlag;

            parameters[1].Value = contractModel.ID;

            int rows = DbHelperSQLite.ExecuteSql(strSql.ToString(), parameters);
        }

        internal void UpdateRealDate(Model.Contract contractModel)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update Contract set ");

            strSql.Append("FinishFlag=@FinishFlag,");
            strSql.Append("RealStartDate=@RealStartDate,");
            strSql.Append("RealEndDate=@RealEndDate ");
            strSql.Append(" where ID=@ID");
            SQLiteParameter[] parameters = {
                    new SQLiteParameter("@FinishFlag", DbType.String),
                    new SQLiteParameter("@RealStartDate", DbType.String),
                    new SQLiteParameter("@RealEndDate", DbType.String),
                    new SQLiteParameter("@ID", DbType.Int32,8)};

            parameters[0].Value = contractModel.FinishFlag;
            parameters[1].Value = contractModel.RealStartDate;
            parameters[2].Value = contractModel.RealEndDate;
            parameters[3].Value = contractModel.ID;

            int rows = DbHelperSQLite.ExecuteSql(strSql.ToString(), parameters);
        }
    }
}